package main

import (
	"github.com/streadway/amqp"
	"os"
	"strings"
)

func bodyform(args []string) string {
	var s string
	if (len(args) < 2) || args[1] == "" {
		s = "hello"
	} else {
		s = strings.Join(args[1:], " ")
	}
	return s
}
func demo() {
	connection, err := amqp.Dial("amqp://guest:guest@localhost:5672/")
	if err != nil {
	}
	defer connection.Close()
	channel, err := connection.Channel()
	if err != nil {
	}
	defer channel.Close()

	//交换器声明
	err = channel.ExchangeDeclare(
		"logs",   //name
		"fanout", //types
		true,     // durable
		false,    // auto-deleted
		false,    // internal
		false,    // no-wait
		nil,      // arguments
	)

	body := bodyform(os.Args)

	//发布
	channel.Publish(
		"logs", //这里不再是空 发布到logs 交换器
		"",     // routing key
		false,  // mandatory
		false,  // immediate
		amqp.Publishing{
			ContentType: "text/plain",
			Body:        []byte(body),
		},
	)
}
func main() {
	demo()
}
